#!/bin/sh
#
# Description:  This shell script takes care of starting and stopping dennis
# Hu Dennis created on Sep. 24th, 2010
#
# Source function library
. /etc/init.d/functions

#the service name  for example: dennis
SNAME=sshd

#the full path and name of the daemon program
#Warning: The name of executable file must be identical with service name
SSHDIR=/usr/local/openssh-arm
PROG=$SSHDIR/sbin/$SNAME

# start function
start() {
    # check /var/empty is exists
    [ -e "/var/empty" ] || mkdir /var/empty
    # check if there is keygen files
	[ -f "$SSHDIR/etc/ssh_host_key" ] || echo "This is your first time to run sshd, Please wait for the keygen..."
    [ -f "$SSHDIR/etc/ssh_host_key" ] || $SSHDIR/bin/ssh-keygen -t rsa1 -f $SSHDIR/etc/ssh_host_key -N ""
    [ -f "$SSHDIR/etc/ssh_host_dsa_key" ] || $SSHDIR/bin/ssh-keygen -t dsa -f $SSHDIR/etc/ssh_host_dsa_key -N ""
    [ -f "$SSHDIR/etc/ssh_host_rsa_key" ] || $SSHDIR/bin/ssh-keygen -t rsa -f $SSHDIR/etc/ssh_host_rsa_key -N ""
    [ -f "$SSHDIR/etc/ssh_host_ecdsa_key" ] || $SSHDIR/bin/ssh-keygen -t ecdsa -f $SSHDIR/etc/ssh_host_ecdsa_key -N ""
    
    #check the daemon status first
    if [ -f /var/lock/subsys/$SNAME ]; then
        echo "$SNAME is already started!"
        exit 0;
    else
        action "Starting $SNAME ..." $PROG
        [ $? -eq 0 ] && touch /var/lock/subsys/$SNAME
        exit 0;
    fi
}

#stop function
stop() {
    echo -n "Stopping $SNAME ..."
    killproc $SNAME
    echo
    rm -rf /var/lock/subsys/$SNAME
}

case "$1" in
start)
  start
  ;;
stop)
  stop
  ;;
reload|restart)
  stop
  start
  ;;
status)
  status $SNAME
  ;;
*)
  echo "Usage: $0 {start|stop|restart|status}"
  exit 1
esac
